package MTClient;

import SQLManager.SQLManager;
import android.R.string;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;

public class LocalPhoneContactManager{
	//private Context context;
	
	public static void CompareLocalDb(Context context){
		
		// A StringBuilder used to build the String
        // that we will display in our LogCat/TextView
		//StringBuilder sb = new StringBuilder();
		
		// Get the Uri for the data returned by the contact picker
        // This Uri identifies the person picked
		ContentResolver cr = context.getContentResolver();
		// Query All of the data from Phone Contacts by Cursor
		Cursor PhoneContactCur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
		
		//return cur;
		if (PhoneContactCur.getCount() > 0) {
            while (PhoneContactCur.moveToNext()) {
                String id = PhoneContactCur.getString(PhoneContactCur.getColumnIndex(BaseColumns._ID));
                String nameOfContact = PhoneContactCur.getString(PhoneContactCur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                if (Integer.parseInt(PhoneContactCur.getString(PhoneContactCur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                  // int phone = 
                	Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id },null);

                    while (phones.moveToNext()) {
                        String contactNumber = phones.getString(phones.getColumnIndex(Phone.NUMBER));
                        String contactNumberRemovedMini = contactNumber.replace("-", "");
                      //  int contactNumberType = phones.getInt(phones.getColumnIndex(Phone.TYPE));
                        System.out.println("...Contact Name ...." + nameOfContact + "...contact Number..." + contactNumber);
                        //Should go to Compare A set on DB
                        System.out.println("isExit method");
                        SQLManager.GetInstance().open();
                        boolean isExist = SQLManager.GetInstance().IsPhoneNumberExist(contactNumberRemovedMini);
                        System.out.println("Next go loop true/false");
                        if (isExist == true){
                        	System.out.println("CONTACT EXCIT");
                        	
                        }
                        	else{	
                        		//update database 
                        		System.out.println("CONTACT NOT EXCIT");	
                        		SQLManager.GetInstance().InsertContactListMember(nameOfContact, contactNumberRemovedMini );
                        		System.out.println("Added,Contact Name ...." + nameOfContact + "...contact Number..." + contactNumber);
                        }
                    }
                    phones.close();
                }

            }
        }// end of contact name cursor
        PhoneContactCur.close();
            //System.out.println(sb.toString());
        
	}

	
	public void UpdateLocalDb(){
		//Add item into Local Contact SQLDB
		SyncServer();
		
	}
	
	public void SyncServer(){
		//.....
	}
	
	
	

}